home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1995 #5 & #6 / Amiga Plus CD - 1995 - No. 5 and 6.iso / pd / netz / amyboard / xboard-3.3.pl0 / cmail.in < prev    next >
Text File  |  1995-08-12  |  66KB  |  1,826 lines

  1. #! @PERLPATH@
  2. ## (configure will change the top line to the location of perl on your system)
  3. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  4. ## cmail $Revision: 3.8 $: a tool to aid playing chess by email
  5. ## Copyright (C) 1993  Free Software Foundation, Inc.
  6. ##
  7. ## cmail is free software; you can redistribute it and/or modify
  8. ## it under the terms of the GNU General Public License as published by
  9. ## the Free Software Foundation; either version 2 of the License, or
  10. ## (at your option) any later version.
  11. ##
  12. ## cmail is distributed in the hope that it will be useful,
  13. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. ## GNU General Public License for more details.
  16. ##
  17. ## You should have received a copy of the GNU General Public License
  18. ## along with cmail; if not, write to the Free Software
  19. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ##
  21. ## Modified:  $Date: 1995/06/24 04:40:54 $
  22. ## Revision:  $Revision: 3.8 $
  23. ## Email:     welsh@epcc.ed.ac.uk
  24. ## Snailmail: Evan Welsh
  25. ##            Edinburgh Parallel Computing Centre
  26. ##            JCMB, Kings Buildings
  27. ##            The University of Edinburgh
  28. ##            Edinburgh EH9 3JZ
  29. ##            Scotland
  30. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  31.  
  32.  
  33. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  34. ## Print verbose diagnostics for debugging
  35. sub debug {
  36.     if ($DEBUG) {
  37.     local ($old) = select ;    ## Remember selected output
  38.     select (logfile) ;
  39.     $| = 1 ;        ## Keep it flushed
  40.     print @_ ;        ## Print arguments
  41.     select ($old) ;
  42.     }
  43. }
  44. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  45.  
  46.  
  47. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  48. ## Create a directory for storing games in if it doesn't already exist
  49. sub need_chess_dir {
  50.     local ($old) ;
  51.  
  52.     ## ################################################################# ##
  53.     ## Check for existence of the named chess directory
  54.     ## ################################################################# ##
  55.  
  56.     if (! (-d "$CMAILDIR")) {
  57.  
  58.     ## ############################################################# ##
  59.     ## Ask user for confirmation if attached to tty
  60.     ## ############################################################# ##
  61.  
  62.     if (-t) {
  63.         $old = select ;    ## Remember selected output
  64.         select (stdout) ;    ## Write to standard output
  65.         $| = 1 ;        ## Keep it flushed
  66.         print (  "CMail directory \"$CMAILDIR\" does not exist."
  67.            . " Create it? [y/q]: ") ;
  68.  
  69.         <tty> ;        ## Read response from tty
  70.         die "Bye!\n" if (/^[qQ].*/) ; ## Quit if q selected
  71.  
  72.         select ($old) ;    ## Re-select the old output
  73.     }
  74.  
  75.     ## ############################################################# ##
  76.     ## Create a cmail directory or die
  77.     ## ############################################################# ##
  78.  
  79.     die "cmail: Can't create CMail directory: \"$CMAILDIR\"\n"
  80.         unless mkdir ("$CMAILDIR", 511) ;
  81.     print (  "Created cmail directory \"$CMAILDIR\".\n"
  82.            . "You can move it but remember to set the CMAIL_DIR"
  83.            . " environment variable.\n") ;
  84.     }
  85.  
  86.     ## ################################################################# ##
  87.     ## Change to the $CMAILDIR directory whether newly created or not
  88.     ## ################################################################# ##
  89.  
  90.     die "Couldn't changed directory to \"$CMAILDIR\"\n"
  91.     unless (chdir "$CMAILDIR") ;
  92.  
  93.     ## ################################################################# ##
  94.     ## Check for existence of the named chess directory
  95.     ## ################################################################# ##
  96.  
  97.     if (! (-d "$ARCDIR")) {
  98.  
  99.     ## ############################################################# ##
  100.     ## Ask user for confirmation if attached to tty
  101.     ## ############################################################# ##
  102.  
  103.     if (-t) {
  104.         $old = select ;    ## Remember selected output
  105.         select (stdout) ;    ## Write to standard output
  106.         $| = 1 ;        ## Keep it flushed
  107.         print (  "Archive directory \"$ARCDIR\" does not exist."
  108.            . " Create it? [y/q]: ") ;
  109.  
  110.         <tty> ;        ## Read response from tty
  111.         die "Bye!\n" if (/^[qQ].*/) ; ## Quit if q selected
  112.  
  113.         select ($old) ;    ## Re-select the old output
  114.     }
  115.  
  116.     ## ############################################################# ##
  117.     ## Create a chess directory or die
  118.     ## ############################################################# ##
  119.  
  120.     die "cmail: Can't create archive directory: \"$ARCDIR\"\n"
  121.         unless mkdir ("$ARCDIR", 511) ;
  122.     print (  "Created archive directory \"$ARCDIR\".\n"
  123.            . "You can move it but remember to set the CMAIL_ARCDIR"
  124.            . " environment variable.\n") ;
  125.     }
  126. }
  127. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  128.  
  129.  
  130. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  131. ## Parse command-line arguments
  132. sub parse_flags {
  133.     ## ################################################################# ##
  134.     ## Set up defaults from the environment or from hard-wired constants
  135.     ## ################################################################# ##
  136.  
  137.     $SHOWC       = 0 ;
  138.     $SHOWW       = 0 ;
  139.     $OUTPUT_POS  = $ENV{'CMAIL_OUTPUT_POS'} ;
  140.     $LOGFILE     = $ENV{'CMAIL_LOGFILE'} ;
  141.     $MAILPROG    = $ENV{'CMAIL_MAILPROG'} ;
  142.     $MAILPROG    = "/usr/ucb/Mail" if (   (-x "/usr/ucb/Mail")
  143.                        && (! $MAILPROG)) ;
  144.     $MAILPROG    = "/usr/ucb/mail" if (   (-x "/usr/ucb/mail")
  145.                        && (! $MAILPROG)) ;
  146.     $MAILPROG    = "Mail" unless ($MAILPROG) ;
  147.     $HOMEDIR     = $ENV{'HOME'} ;
  148.     $CMAILDIR     = $ENV{'CMAIL_DIR'} ;
  149.     $CMAILDIR     = $ENV{'CHESSDIR'} unless ($CMAILDIR) ;
  150.     $CMAILDIR     = "$HOMEDIR/Chess" unless ($CMAILDIR) ;
  151.     $CMAILDIR     = "~/Chess"        unless ($HOMEDIR) ;
  152.     $NUM_GAMES     = "?" ;
  153.     $NUM_WGAMES     = "?" ;
  154.     $NUM_BGAMES     = "?" ;
  155.     $TIME_DELAY     = $ENV{'CMAIL_TIME_DELAY'} ;
  156.     $TIME_DELAY     = 0 unless ($TIME_DELAY) ;
  157.     $PW_NAME     = &get_pw_name () ;
  158.     $MY_NNAME     = $PW_NAME ;
  159.     $MY_NNAME     = $ENV{'LOGNAME'} unless ($MY_NNAME) ;
  160.     $MY_NNAME     = $ENV{'USER'} unless ($MY_NNAME) ;
  161.     $MY_NNAME     = "?" unless ($MY_NNAME) ;
  162.     $PGN_EVENT   = "Email correspondence game" ;
  163.     $PGN_SITE     = "NET";
  164.     $PGN_ROUND     = "-";
  165.     $PGN_MODE    = "EM";
  166.     $SEND_MAIL   = 1 ;
  167.     $REMAIL      = 0 ;
  168.     $LOAD_XBOARD = 1 unless $ENV{'CMAIL_NO_XBOARD'} ;
  169.     $REUSE       = 1 ;
  170.     @TD_FLAGS    = ("-td", $TIME_DELAY) ;
  171.     @NCP_FLAGS   = ("-ncp") ;
  172.  
  173.     ## ################################################################# ##
  174.     ## Define the usage string
  175.     ## ################################################################# ##
  176.  
  177.     $USAGE = ("cmail
  178.         [-h] [-c] [-w] [-[x]v] [-[x]mail] [-[x]xboard] [-[x]reuse] [-remail] 
  179.         [-game <name>] [-(w|b|)games <number>] [-(me|opp) <short name>]
  180.         [-(w|b|my|opp)name <full name>] [-(w|b|my|opp)na <email>]
  181.         [-dir <directory>] [-arcdir <directory>] [-mailprog <mail program>]
  182.         [-logFile <file>] [-event <event>] [-site <site>] [-round <round>]
  183.     [-mode <mode>]") ;
  184.  
  185.     ## ################################################################# ##
  186.     ## Overwrite defaults if specified on the command-line
  187.     ## ################################################################# ##
  188.  
  189.     @UNREC_ARGS = () ;
  190.     while ($ARGV = shift) {
  191.     $UNREC = 0 if ($ARGV =~ /^-/) ;
  192.     if    ("$ARGV" eq "-h")          {die ("Usage: $USAGE\n")     ;}
  193.     elsif ("$ARGV" eq "-c")          {$SHOWC       = 1            ;}
  194.     elsif ("$ARGV" eq "-w")          {$SHOWW       = 1            ;}
  195.     elsif ("$ARGV" eq "-v")          {$DEBUG       = 1            ;
  196.                            @DEBUG_FLAGS = ("-debug")   ;}
  197.     elsif ("$ARGV" eq "-xv")      {$DEBUG       = 0            ;
  198.                            $QUIET       = 1            ;}
  199.     elsif ("$ARGV" eq "-mail")      {$SEND_MAIL   = 1            ;}
  200.     elsif ("$ARGV" eq "-xmail")      {$SEND_MAIL   = 0            ;}
  201.     elsif ("$ARGV" eq "-xboard")      {$LOAD_XBOARD = 1            ;}
  202.     elsif ("$ARGV" eq "-xxboard")      {$LOAD_XBOARD = 0            ;}
  203.     elsif ("$ARGV" eq "-reuse")      {$REUSE       = 1            ;}
  204.     elsif ("$ARGV" eq "-xreuse")      {$REUSE       = 0            ;}
  205.     elsif ("$ARGV" eq "-remail")      {$LOAD_XBOARD = 0            ;
  206.                        $SEND_MAIL   = 1            ;
  207.                        $REMAIL      = 1            ;}
  208.     elsif ("$ARGV" eq "-game")      {$PGN_GAME    = shift        ;}
  209.     elsif ("$ARGV" eq "-games")       {$NUM_GAMES   = shift        ;}
  210.     elsif ("$ARGV" eq "-wgames")      {$NUM_WGAMES  = shift        ;}
  211.     elsif ("$ARGV" eq "-bgames")      {$NUM_BGAMES  = shift        ;}
  212.     elsif ("$ARGV" eq "-me")      {$MY_NNAME    = shift        ;}
  213.     elsif ("$ARGV" eq "-opp")      {$OPP_NNAME   = shift        ;}
  214.     elsif ("$ARGV" eq "-myname")      {$MY_FNAME    = shift        ;}
  215.     elsif ("$ARGV" eq "-oppname")      {$OPP_FNAME   = shift        ;}
  216.     elsif ("$ARGV" eq "-wname")      {$WHITE_FNAME    = shift           ;}
  217.     elsif ("$ARGV" eq "-bname")      {$BLACK_FNAME    = shift           ;}
  218.     elsif ("$ARGV" eq "-myna")      {$MY_ADDRESS  = shift        ;}
  219.     elsif ("$ARGV" eq "-oppna")      {$OPP_ADDRESS = shift        ;}
  220.     elsif ("$ARGV" eq "-wna")      {$WHITENA    = shift           ;}
  221.     elsif ("$ARGV" eq "-bna")      {$BLACKNA    = shift           ;}
  222.     elsif ("$ARGV" eq "-dir")      {$CMAILDIR    = shift        ;}
  223.     elsif ("$ARGV" eq "-arcdir")      {$ARCDIR      = shift        ;}
  224.     elsif ("$ARGV" eq "-mailprog")      {$MAILPROG    = shift        ;}
  225.     elsif ("$ARGV" eq "-logFile")     {$LOGFILE     = shift        ;}
  226.     elsif ("$ARGV" =~ /^-(td|timeDelay)$/)
  227.                                       {@TD_FLAGS    = ($ARGV,
  228.                                shift)      ;}
  229.     elsif ("$ARGV" =~ /^-noChessComputer$/)
  230.                                       {@NCP_FLAGS   = ($ARGV,
  231.                                shift)      ;}
  232.     elsif ("$ARGV" =~ /^-[x]?ncp$/)      {@NCP_FLAGS    = ($ARGV)      ;}
  233.     elsif ("$ARGV" eq "-event")      {@PGN_EVENT    = (shift)      ;}
  234.     elsif ("$ARGV" eq "-site")      {@PGN_SITE    = (shift)      ;}
  235.     elsif ("$ARGV" eq "-round")      {@PGN_ROUND    = (shift)      ;}
  236.     elsif ("$ARGV" eq "-mode")      {@PGN_MODE    = (shift)      ;}
  237.     elsif ("$ARGV" =~ /^-/ || $UNREC) {
  238.         push(@UNREC_ARGS, $ARGV) ;
  239.         $UNREC = 1 ;
  240.     } else {
  241.         die("cmail: Unrecognised flag \"$ARGV\"\nUsage: $USAGE\n") ;
  242.     }
  243.     }
  244.  
  245.     ## ################################################################# ##
  246.     ## Assign a value to $ARCDIR if not specified on the command line
  247.     ## ################################################################# ##
  248.  
  249.     $ARCDIR = $ENV{'CMAIL_ARCDIR'} unless ($ARCDIR) ;
  250.     $ARCDIR = $CMAILDIR unless ($ARCDIR) ;
  251.     $ENV{'CMAIL_ARCDIR'} = $ARCDIR ; ## Make sure this is set for xboard
  252.  
  253.     ## ################################################################# ##
  254.     ## Propagate some CMAIL variables through xboard to the cmail
  255.     ## grandchild so that it uses the same important variables as this one
  256.     ## ################################################################# ##
  257.  
  258.     $ENV{'CMAIL_MAILPROG'} = $MAILPROG ;
  259.     $ENV{'CMAIL_DIR'}      = $CMAILDIR ;
  260.     $ENV{'CHESSDIR'}       = $CMAILDIR ; ## Make xboard use $CMAILDIR
  261.     $ENV{'CMAIL_ARCDIR'}   = $ARCDIR ;
  262.     if ($LOGFILE) {
  263.     $ENV{'CMAIL_LOGFILE'}  = $LOGFILE ;
  264.     } else {
  265.     $LOGFILE = "&STDERR" ;
  266.     }
  267.  
  268.     ## ################################################################# ##
  269.     ## Work out how many games of each colour will be played
  270.     ## ################################################################# ##
  271.  
  272.     die "cmail: Illegal number of games: $NUM_GAMES\n"
  273.     if ($NUM_GAMES < 0) ;
  274.     die "cmail: Illegal number of white games: $NUM_WGAMES\n"
  275.     if ($NUM_WGAMES < 0) ;
  276.     die "cmail: Illegal number of black games: $NUM_BGAMES\n"
  277.     if ($NUM_BGAMES < 0) ;
  278.     if ("$NUM_GAMES" ne "?") {
  279.     if ("$NUM_WGAMES" eq "?") {
  280.         if ("$NUM_BGAMES" eq "?") {
  281.         $NUM_BGAMES = int($NUM_GAMES / 2) ;
  282.         }
  283.         $NUM_WGAMES = $NUM_GAMES - $NUM_BGAMES ;
  284.     } elsif ("$NUM_BGAMES" eq "?") {
  285.         $NUM_BGAMES = $NUM_GAMES - $NUM_WGAMES ;
  286.     }
  287.     } elsif ("$NUM_WGAMES" eq "?") {
  288.     if ("$NUM_BGAMES" eq "?") {
  289.         $NUM_GAMES  = 1 ;
  290.         $NUM_WGAMES = 1 ;
  291.         $NUM_BGAMES = 0 ;
  292.     } else {
  293.         $NUM_GAMES  = $NUM_BGAMES ;
  294.         $NUM_WGAMES = 0 ;
  295.     }
  296.     } else {
  297.     if ("$NUM_BGAMES" eq "?") {
  298.         $NUM_GAMES  = $NUM_WGAMES ;
  299.         $NUM_BGAMES = 0 ;
  300.     } else {
  301.         $NUM_GAMES  = $NUM_WGAMES + $NUM_BGAMES ;
  302.     }
  303.     }
  304.     die "cmail: Illegal number of games: $NUM_GAMES\n"
  305.     if ("$NUM_GAMES" eq "0") ;
  306.     die (  "cmail: Inconsistent numbers of games specified:"
  307.      . " $NUM_WGAMES + $NUM_BGAMES != $NUM_GAMES\n")
  308.     unless ($NUM_GAMES == $NUM_WGAMES + $NUM_BGAMES) ;
  309. }
  310. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  311.  
  312.  
  313. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  314. ## Initialisation of variables and environment
  315. sub showGPL {
  316.     ## ################################################################# ##
  317.     ## Show copyright notice
  318.     ## ################################################################# ##
  319.  
  320.     while (<DATA>) {
  321.     last if (/^{END OF GPL COPYRIGHT}$/) ;
  322.     s/\$Revision[:] (.*) \$/$1/ ;
  323.     print ;
  324.     }
  325.  
  326.     ## ################################################################# ##
  327.     ## Show conditions if requested
  328.     ## ################################################################# ##
  329.  
  330.     while (<DATA>) {
  331.     last if (/^{END OF GPL CONDITIONS}$/) ;
  332.     print if ($SHOWW) ;
  333.     }
  334.  
  335.     ## ################################################################# ##
  336.     ## Show warranty if requested
  337.     ## ################################################################# ##
  338.  
  339.     if ($SHOWC) {
  340.     print "\n" if ($SHOWW) ;
  341.     print while (<DATA>) ;
  342.     }
  343.  
  344.     
  345.     exit 0 if ($SHOWC || $SHOWW) ; ## Abort if showed conditions or warranty
  346. }
  347. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  348.  
  349.  
  350. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  351. ## Initialisation of variables and environment
  352. sub initialise {
  353.     local ($p) = "[.PRNBQKprnbqk]" ;
  354.     local ($l) = "$p $p $p $p $p $p $p $p\n" ;
  355.     local ($board) = "$l$l$l$l$l$l$l$l" ;
  356.     local ($tp) = ".* to play\n" ;
  357.     $posdiag = "\{--------------\n$board$tp--------------\}\n+" ;
  358.  
  359.     &parse_flags (@ARGV) ;    ## Parse command-line arguments
  360.  
  361.     &showGPL unless $QUIET ;
  362.  
  363.     open (tty, "< /dev/tty") ;    ## Open tty for reading
  364.  
  365.     &need_chess_dir () ;    ## Check for the existence of CMAILDIR
  366.  
  367.     open (logfile, ">$LOGFILE") if ($DEBUG) ; ## Default is STDERR
  368.  
  369.     &debug ("Called <initialise>\n") ;
  370. }
  371. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  372.  
  373.  
  374. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  375. ## Prompt for a game name, if <cr> use a default
  376. sub prompt_for_game_name {
  377.     &debug ("Called <prompt_for_game_name>\n") ;
  378.     local ($old) = select ;    ## Remember the selected output
  379.     select (stdout);        ## Prompt goes to stdout
  380.     $| = 1 ;            ## Keep it flushed
  381.  
  382.     print "Game name [<cr> to use default]: " ;
  383.     die "cmail: tty not open\n" unless (-t) ;
  384.     <tty> =~ /(.*)/ ;        ## Read line from tty
  385.     $PGN_GAME = "$1" ;        ## Assign to game name
  386.     
  387.     select ($old) ;        ## Re-select the old output
  388. }
  389. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  390.  
  391.  
  392. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  393. ## Prompt for opponent's address
  394. sub prompt_for_opp_address {
  395.     &debug ("Called <prompt_for_opp_address>\n") ;
  396.     local ($old) = select ;    ## Remember the selected output
  397.     select (stdout);        ## Prompt goes to stdout
  398.     $| = 1 ;            ## Keep it flushed
  399.  
  400.     ## ################################################################# ##
  401.     ## Prompt for opponent's email address
  402.     ## ################################################################# ##
  403.  
  404.     print "Opponent's email address: " ;
  405.     die "cmail: tty not open\n" unless (-t) ;
  406.     <tty> =~ /(.*)/ ;
  407.     $OPP_ADDRESS = $1 ;
  408.  
  409.     ## ################################################################# ##
  410.     ## Use name as default if still blank
  411.     ## ################################################################# ##
  412.  
  413.     $OPP_ADDRESS = $OPP_NNAME if ("" eq $OPP_ADDRESS) ;
  414.     
  415.     select ($old) ;        ## Re-select the old output
  416. }
  417. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  418.  
  419.  
  420. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  421. ## Prompt for opponent's name
  422. sub prompt_for_opp_name {
  423.     &debug ("Called <prompt_for_opp_name>\n") ;
  424.     local ($old) = select ;    ## Remember the selected output
  425.     select (stdout) ;        ## Prompt goes to stdout
  426.     $| = 1 ;            ## Keep it flushed
  427.     print "Opponent's name: " ;
  428.  
  429.     die "cmail: tty not open\n" unless (-t) ; ## Check tty is open
  430.     <tty> =~ /(.*)/ ;        ## Read line from tty
  431.     $OPP_NNAME = $1 ;        ## Match!
  432.     die "cmail: Can't proceed without the opponent's name.\n"
  433.     unless ($OPP_NNAME) ;
  434.  
  435.     select ($old) ;        ## Re-select the old output
  436. }
  437. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  438.  
  439.  
  440. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  441. ## Prompt for move
  442. sub prompt_for_move {
  443.     local ($prompt) = shift ;
  444.     local ($pattern) = shift ;
  445.     &debug ("Called <prompt_for_move>\n") ;
  446.  
  447.     local ($move) = "" ;
  448.     local ($old) = select ;    ## Remember the selected output
  449.     select (stdout) ;        ## Write to stdout
  450.     $| = 1 ;            ## Keep it flushed
  451.     die "cmail: tty not open\n" unless (-t) ; ## Check tty is open
  452.  
  453.     do {
  454.     print $prompt ;
  455.     <STDIN> =~ /(.*)/ ;    ## Read line from tty
  456.     $move = $1 ;        ## Match!
  457.     } until ($move =~ /^$pattern$/) ;
  458.  
  459.     select ($old) ;        ## Re-select the old output
  460.     return ($move) ;
  461. }
  462. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  463.  
  464.  
  465. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  466. ## Load the game
  467. sub play_game {
  468.     &debug ("Called <play_game>\n") ;
  469.  
  470.     $| = 1 ;            ## Start flushing output buffer
  471.  
  472.     ## ################################################################# ##
  473.     ## Load xboard unless inhibited by command-line arguments
  474.     ## ################################################################# ##
  475.  
  476.     if (($STARTING_NEW_GAME) && ($NUM_WGAMES == 0)) {
  477.     print (  "Bypassing xboard and mailing $NUM_BGAMES empty"
  478.            . " black games.\n") ;
  479.     } elsif ($LOAD_XBOARD) {
  480.     ## ############################################################# ##
  481.     ## Remove output file from previous run, but preserve
  482.     ## $PGN_GAME.game.out.* because they will be empty black games
  483.     ## ############################################################# ##
  484.  
  485.     unlink "$PGN_GAME.out" ;
  486.  
  487.     ## ############################################################# ##
  488.     ## Invoke xboard with loads of flags
  489.     ## ############################################################# ##
  490.  
  491.     if ($PGN_GAME) {
  492.         if (@ARCHIVE) {
  493.         local ($date) = &get_date_from_games (@ARCHIVE) ;
  494.         $XBOARD_ARGS = join (' ', (("-lgf",
  495.                         "$ARCDIR/$PGN_GAME.$date.archive"),
  496.                        "-ncp",
  497.                        "-xics",
  498.                        @TD_FLAGS,
  499.                        @DEBUG_FLAGS,
  500.                        @UNREC_ARGS)) ;
  501.         } else {
  502.         $XBOARD_ARGS = join (' ', (("-cmail", $PGN_GAME),
  503.                        @TD_FLAGS,
  504.                        @NCP_FLAGS,
  505.                        "-xics",
  506.                        @DEBUG_FLAGS,
  507.                        @UNREC_ARGS)) ;
  508.         }
  509.     } else {
  510.         $PGN_GAME = "unknown.cmail" ;
  511.         $XBOARD_ARGS = join (' ', (("-lgf", $PGN_GAME),
  512.                        "-ncp",
  513.                        "-xics",
  514.                        @TD_FLAGS,
  515.                        @DEBUG_FLAGS,
  516.                        @UNREC_ARGS)) ;
  517.         $REUSE = 0 ;
  518.     }
  519.  
  520.     $LOG_FILE = "$PGN_GAME.log" ;
  521.     &debug ("Invoking xboard with args: $XBOARD_ARGS\n") ;
  522.     $PID_FILE = "$PGN_GAME.pid" ;
  523.     if (   (! $REUSE)
  524.         || (! (   (-f $PID_FILE)
  525.            && ($XBOARD_PID = `cat $PID_FILE`)
  526.            && ("$XBOARD_PID" =~ /^\d+$/)
  527.            && (kill "SIGUSR1", $XBOARD_PID)))) {
  528.         print "Loading xboard for game \"$PGN_GAME\"..." ;
  529. #          system ("gdb xboard") ;
  530.         system (  "{ ({ xboard $XBOARD_ARGS & } ;"
  531.             . "   echo \$! > $PID_FILE ;"
  532.             . "   wait ;"
  533.             . "   rm $PID_FILE) & } >$LOG_FILE 2>&1") ;
  534.         print (  "done.\n"
  535.            . "If nothing happens look for an error message in\n"
  536.            . "$CMAILDIR/$LOG_FILE\n") ;
  537.     } else {
  538.         print ("Revived existing xboard for game \"$PGN_GAME\".\n"
  539.            . "If nothing happens"
  540.            . " remove $CMAILDIR/$PID_FILE and try again.\n") ;
  541.     }
  542.  
  543.     return 1 ;
  544.     }
  545.     return 0 ;
  546. }
  547. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  548.  
  549.  
  550. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  551. ## Enter moves on tty instead of xboard
  552. sub play_on_tty {
  553.     &debug ("Called <play_on_tty>\n") ;
  554.  
  555.     local (@results) = @_ ;
  556.  
  557.     local (@games, $game, $to_play) ;
  558.  
  559.     ## ################################################################# ##
  560.     ## Check we have access to tty
  561.     ## ################################################################# ##
  562.  
  563.     if (open(STDIN, '/dev/tty')) {
  564.     ## ############################################################# ##
  565.     ## Read in games
  566.     ## ############################################################# ##
  567.     
  568.     local ($infile) = "$PGN_GAME.game.in" ;
  569.     if (-f $infile) {
  570.         @games = &get_games_from_file($infile) ;
  571.         &debug ("Read in games from \"$infile\"\n") ;
  572.  
  573.         if (@results) {
  574.         foreach $game (@games) {
  575.             $result = shift (@results) ;
  576.             if ($result && ($game =~ /\[Result\s+"[^*]+"\]/)) {
  577.             $game = "" ;
  578.             }
  579.         }
  580.         }
  581.  
  582.         ## ################################################################# ##
  583.         ## Accept move on tty for each game in turn
  584.         ## ################################################################# ##
  585.         
  586.         local ($comment_orig, $comment, $comment_line,
  587.            $pos, $move, $normal_move, $action, $result) ;
  588.  
  589.         foreach $game (@games) {
  590.         next unless $game ;
  591.  
  592.         $game =~ s/[\s\n]*[*]?[\s\n]*$// ;
  593.         $pos = "" ;
  594.         $pos = $1 if ($game =~ s/($posdiag)//) ;
  595.         print $game, "\n\n", $pos ;
  596.  
  597.         ($number, $to_play) = &get_to_play ($game) ;
  598.  
  599.         $comment_orig = "" ;
  600.         $game_nocomment = $game ;
  601.         if ($game_nocomment =~ s/\n?{\n?([^{]*)}$//) {
  602.             $comment_orig = $1 ;
  603.             $comment_orig =~ s/([^\n])$/$1\n/ ;
  604. #              &debug ("Comment is:\n{\n$comment_orig}\n") ;
  605.         }
  606.         if ($game =~ /\[Result\s+"[^*]+"\]/) {
  607.             &prompt_for_move ("Game finished, press \"Return\" to continue: ", "") ;
  608.                     next ;
  609.         }
  610.  
  611.         $tmpgame = $game_nocomment ;
  612.         $comment = $comment_orig ;
  613.  
  614.           outer:
  615.         while (1) {
  616.             if ($game =~ /{\n?.* offers a draw\n?}$/) {
  617.             $move = &prompt_for_move ("Enter move [MOVE/(r)esign/(a)ccept/(c)omment/re(t)ry]: ",
  618.                           "([-a-h0-9PRNBQK][-a-h0-9PRNBQK]+|[ract])") ;
  619.             } else {
  620.             $move = &prompt_for_move ("Enter move [MOVE/(r)esign/(c)omment/re(t)ry]: ",
  621.                           "([-a-h0-9PRNBQK][-a-h0-9PRNBQK]+|[rct])") ;
  622.             }
  623.             $normal_move = 0 ;
  624.             $result = "" ;
  625.             if ($move =~ /^\s*r\s*$/i) {
  626.             if ($to_play eq "White") {
  627.                 $result = "0-1" ;
  628.             } else {
  629.                 $result = "1-0" ;
  630.             }
  631.             $move = "\n{$to_play resigns} $result" ;
  632.             } elsif ($move =~ /^\s*a\s*$/i) {
  633.             $move = "\n{Draw agreed} $result" ;
  634.             } elsif ($move =~ /^\s*c\s*$/i) {
  635.                         while ($comment_line = &prompt_for_move ("Enter comment: ", ".*")) {
  636.                 $comment .= $comment_line . "\n" ;
  637.                         }
  638.             next ;
  639.             } elsif ($move =~ /^\s*t\s*$/i) {
  640.             print $game, "\n\n", $pos ;
  641.             $tmpgame = $game_nocomment ;
  642.             $comment = $comment_orig ;
  643.             print "Try again.\n" ;
  644.             next ;
  645.             } else {
  646.             $normal_move = 1 ;
  647.             }
  648.  
  649.             $tmpgame .= "\n{\n" . $comment . "}" if ($comment) ;
  650.             if (! $normal_move) {
  651.             $tmpgame .= "$move"  ;
  652.             } elsif ($to_play eq "White") {
  653.             $tmpgame .= "\n$number. $move"  ;
  654.             } elsif ($tmpgame =~ /}$/) {
  655.             $tmpgame .= "\n$number. ... $move"  ;
  656.             } else {
  657.             $tmpgame .= " $move"  ;
  658.             }
  659.  
  660.             $tmpgame =~ s/\[Result\s+"(.*)"\]/[Result "$result"]/ if ($result) ;
  661.             $comment = "" ;
  662.           middle:
  663.             while (1) {
  664.             if ($normal_move) {
  665.                 $action = &prompt_for_move ("Enter action [(d)raw/(c)omment/(s)end/re(t)ry]: ",
  666.                             "[dcst]") ;
  667.             } elsif ($result) {
  668.                 $action = &prompt_for_move ("Enter action [(s)end/re(t)ry]: ",
  669.                             "[st]") ;
  670.             } else {
  671.                 $action = &prompt_for_move ("Enter action [(c)omment/(s)end/re(t)ry]: ",
  672.                             "[cst]") ;
  673.             }
  674.             if ($action =~ /^\s*d\s*$/i) {
  675.                 if ($normal_move) {
  676.                 $comment .= "$to_play offers a draw\n" ;
  677.  
  678.                 while (1) {
  679.                     $action = &prompt_for_move ("Enter action [(c)omment/(s)end/re(t)ry]: ",
  680.                                 "[cst]") ;
  681.                     if ($action =~ /^\s*c\s*$/i) {
  682.                     while ($comment_line = &prompt_for_move ("Enter comment: ", ".*")) {
  683.                         $comment .= $comment_line . "\n" ;
  684.                                 }
  685.                         next ;
  686.                     } elsif ($action =~ /^\s*t\s*$/i) {
  687.                     print $game, "\n\n", $pos ;
  688.                     $tmpgame = $game_nocomment ;
  689.                     $comment = $comment_orig ;
  690.                     print "Try again.\n" ;
  691.                     next outer;
  692.                     } elsif ($action =~ /^\s*s\s*$/i) {
  693.                     $tmpgame .= "\n{\n" . $comment . "}" ;
  694.                     last middle ;
  695.                     }
  696.                 }
  697.                 } else {
  698.                 print "You can't offer a draw at this point.\n" ;
  699.                 next ;
  700.                 }
  701.             } elsif ($action =~ /^\s*c\s*$/i) {
  702.                 if ($result) {
  703.                 print "You can't enter a comment after the game is finished.\n" ;
  704.                 } else {
  705.                 while ($comment_line = &prompt_for_move ("Enter comment: ", ".*")) {
  706.                     $comment .= $comment_line . "\n" ;
  707.                             }
  708.                 }
  709.                 next ;
  710.             } elsif ($action =~ /^\s*t\s*$/i) {
  711.                 print $game, $pos ;
  712.                 $tmpgame = $game_nocomment ;
  713.                 $comment = $comment_orig ;
  714.                 print "Try again.\n" ;
  715.                 next outer;
  716.             } elsif ($action =~ /^\s*s\s*$/i) {
  717.                 $tmpgame .= "\n{\n" . $comment . "}\n" if ($comment) ;
  718.                 last ;
  719.             }
  720.             }
  721.  
  722.             last ;
  723.         }
  724.  
  725.         $tmpgame .= "\n*" if ($normal_move) ;
  726.         $tmpgame .= "\n\n" ;
  727.         $game = $tmpgame ;
  728.         }
  729.     } else {
  730.         die "cmail: No games to be read\n" ;
  731.     }
  732.     } else {
  733.     die "cmail: Can't open tty" ;
  734.     }
  735.  
  736.     return (@games) ;
  737. }
  738. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  739.  
  740.  
  741. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  742. ## Find the game the user wants by any means possible
  743. sub find_game {
  744.     &debug ("Called <find_game>\n") ;
  745.  
  746.     ## ################################################################# ##
  747.     ## Ask user for a game name if not already known
  748.     ## ################################################################# ##
  749.  
  750.     &prompt_for_game_name () if ("" eq "$PGN_GAME") ;
  751.  
  752.     ## ################################################################# ##
  753.     ## Find out opponent's details
  754.     ## ################################################################# ##
  755.  
  756.     if ("" eq "$PGN_GAME") {
  757.     ## ############################################################# ##
  758.     ## Failed to find the game name so construct a default from players
  759.     ## ############################################################# ##
  760.     
  761.     &prompt_for_opp_name () if ("" eq "$OPP_NNAME") ; ## Ask user
  762.     die "cmail: Can't proceed without your opponent's short name (-opp)\n"
  763.         if ("" eq "$OPP_NNAME") ;
  764.     die "cmail: Can't proceed without your own short name (-me)\n"
  765.         if ("" eq "$MY_NNAME") ;
  766.     if ($NUM_WGAMES > 0) {
  767.         $PGN_GAME = "$MY_NNAME-vs-$OPP_NNAME" ; ## Construct default
  768.     } else {
  769.         $PGN_GAME = "$OPP_NNAME-vs-$MY_NNAME" ; ## Construct default
  770.     }
  771.     } elsif (("" eq "$OPP_ADDRESS") && ("" ne "$RETURN_ADDRESS")) {
  772.     $OPP_ADDRESS = $RETURN_ADDRESS ; ## Use return address instead
  773.     &debug (  "Using return address \"$OPP_ADDRESS\""
  774.         . " for opponent address\n") ;
  775.     }
  776.  
  777.     ## ################################################################# ##
  778.     ## If no $PGN_GAME.game.in file, assume we're starting a new game
  779.     ## ################################################################# ##
  780.  
  781.     &start_new_game () unless (-f "$PGN_GAME.game.in") ;
  782. }
  783. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  784.  
  785.  
  786. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  787. ## Get the date
  788. sub get_date {
  789.     local ($the_time) = time ;
  790.     local ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
  791.     localtime ($the_time) ;
  792.     $mon ++ ;
  793.     if ($mon < 10) {
  794.     $mon = "0$mon" ;
  795.     }
  796.     if ($year < 94) {
  797.     $century = 20 ;
  798.     } else {
  799.     $century = 19 ;
  800.     }
  801.     "$century$year.$mon.$mday" ;
  802. }
  803. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  804.  
  805.  
  806. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  807. ## Start a new game
  808. sub start_new_game {
  809.     print (  "Starting new game"
  810.        . " -- $NUM_WGAMES as white, $NUM_BGAMES as black.\n") ;
  811.     local ($to_play) = "white" ;
  812.     $move_num = 0 ;
  813.     $STARTING_NEW_GAME = 1 ;
  814.  
  815.     local (@game) ;
  816.  
  817.     ## ################################################################# ##
  818.     ## Ask user for opponent's email address if unknown
  819.     ## ################################################################# ##
  820.  
  821.     &prompt_for_opp_address () if ("" eq "$OPP_ADDRESS") ;
  822.  
  823.     ## ################################################################# ##
  824.     ## Give up if we haven't got anywhere to send a move to
  825.     ## ################################################################# ##
  826.  
  827.     die "cmail: Can't proceed without your opponent's email address.\n"
  828.     if ("" eq "$OPP_ADDRESS") ;
  829.  
  830.     ## ################################################################# ##
  831.     ## Create an empty game file
  832.     ## ################################################################# ##
  833.  
  834.     open (GAMEFILE, "> $PGN_GAME.game.in") ;
  835.     for ($j = 1; $j <= $NUM_GAMES; $j ++) {
  836.     $PW_GCOS      =  &get_pw_gcos () ;
  837.  
  838.     $PGN_MYCOL    =  $MY_FNAME ;
  839.     $PGN_MYCOL    =  $PW_GCOS unless $PGN_MYCOL ;
  840.     $PGN_MYCOL    =  $MY_NNAME unless $PGN_MYCOL ;
  841.     $PGN_MYCOLNA  =  $MY_ADDRESS ;
  842.     $PGN_MYCOLNA  =  "?" unless ($PGN_MYCOLNA) ;
  843.                     
  844.     $PGN_OPPCOL   =  $OPP_FNAME ;
  845.     $PGN_OPPCOL   =  "?" unless ($PGN_OPPCOL) ;
  846.     $PGN_OPPCOLNA =  $OPP_ADDRESS ;
  847.     $PGN_OPPCOLNA =  "?" unless ($PGN_OPPCOLNA) ;
  848.     
  849.     if ($j > $NUM_WGAMES) {
  850.         $PGN_WHITE   = $PGN_OPPCOL ;
  851.         $PGN_BLACK   = $PGN_MYCOL ;
  852.         $PGN_WHITENA = $PGN_OPPCOLNA ;
  853.         $PGN_BLACKNA = $PGN_MYCOLNA ;
  854.     } else {
  855.         $PGN_WHITE   = $PGN_MYCOL ;
  856.         $PGN_BLACK   = $PGN_OPPCOL ;
  857.         $PGN_WHITENA = $PGN_MYCOLNA ;
  858.         $PGN_BLACKNA = $PGN_OPPCOLNA ;
  859.     }
  860.     
  861.         ## ######################################################### ##
  862.     ## If we only have one colour of game then allow command-line 
  863.     ## colour specs to override
  864.     ## ######################################################### ##
  865.     
  866.     if (! ($NUM_WGAMES && $NUM_BGAMES)) {
  867.         $PGN_WHITE   = $WHITE_FNAME if ($WHITE_FNAME) ;
  868.         $PGN_BLACK   = $BLACK_FNAME if ($BLACK_FNAME) ;
  869.         $PGN_WHITENA = $WHITENA     if ($WHITENA)     ;
  870.         $PGN_BLACKNA = $BLACKNA     if ($BLACKNA)     ;
  871.     }
  872.     
  873.     $PGN_DATE = &get_date () ;
  874.     $PGN_DATE = "?" unless ($PGN_DATE) ;
  875.  
  876.     if ($NUM_GAMES > 1) {
  877.         $SUFFIX = ".$j" ;
  878.     } else {
  879.         $SUFFIX = "" ;
  880.     }
  881.     @game = ("[Event \"$PGN_EVENT\"]\n",
  882.          "[Site \"$PGN_SITE\"]\n",
  883.          "[Date \"$PGN_DATE\"]\n",
  884.          "[Round \"$PGN_ROUND\"]\n",
  885.          "[White \"$PGN_WHITE\"]\n",
  886.          "[Black \"$PGN_BLACK\"]\n",
  887.          "[Result \"*\"]\n",
  888.          "[WhiteNA \"$PGN_WHITENA\"]\n",
  889.          "[BlackNA \"$PGN_BLACKNA\"]\n",
  890.          "[Mode \"$PGN_MODE\"]\n",
  891.          "[CmailGameName \"$PGN_GAME$SUFFIX\"]\n\n*\n") ;
  892.     if ($j > $NUM_WGAMES) {
  893.         open (GAMEOUTFILE, "> $PGN_GAME.game.out.$j") ;
  894.         print GAMEOUTFILE @game ;
  895.         close (GAMEOUTFILE) ;
  896.     } else {
  897.         print GAMEFILE @game ;
  898.     }
  899.     }
  900.     close (GAMEFILE) ;
  901. }
  902. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  903.  
  904.  
  905. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  906. ## Get the password file gcos (full name) entry
  907. sub get_pw_entry {
  908.     local ($name, $passwd, $uid, $gid, $quota, $comment, $gcos, $dir, $shell) =
  909.     getpwuid ($<);
  910.     ($name, $gcos) ;
  911. }
  912. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  913.  
  914.  
  915. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  916. ## Get password file gcos (full name) entry
  917. sub get_pw_gcos {
  918.     local ($PW_GCOS) ;
  919.     if (! $PW_GCOS) {
  920.     ($dummy, $PW_GCOS) = &get_pw_entry () ;
  921.     $PW_GCOS =~ s/^\s*([^,()]+[^ ,()])[ ]*[,()].*$/$1/;
  922.     if ($PW_GCOS =~ /^([^,()]+)\s+([^\s,()]+)$/) { ## Multi-word name
  923.         $PW_GCOS = $2 . ", " . $1 ;
  924.     } elsif ($PW_GCOS !~ /^([^\s,()]+)/) { ## No sensible gcos entry 
  925.         $PW_GCOS = "" ;
  926.     }            ## Else leave it as one word
  927.     &debug ("PW full name is \"$PW_GCOS\"\n");
  928.     }
  929.     return $PW_GCOS ;
  930. }
  931. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  932.  
  933.  
  934. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  935. ## Get password file user name entry
  936. sub get_pw_name {
  937.     local ($PW_NAME) ;
  938.     ($PW_NAME, $dummy) = &get_pw_entry () ;
  939.     &debug ("PW name is $PW_NAME\n");
  940.  
  941.     return $PW_NAME ;
  942. }
  943. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  944.  
  945.  
  946. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  947. ## Analyse the email message
  948. sub analyse_email_message {
  949.     local ($new_result, $delete_result, $unfinished, $finished)
  950.     = (0, 0, 0, 0) ;
  951.     local ($gamefile, @games) ;
  952.  
  953.     ## ################################################################# ##
  954.     ## Slurp the mail message
  955.     ## ################################################################# ##
  956.  
  957.     die "cmail: No games on standard input" 
  958.     unless (@games = &get_games_from_file ("STDIN")) ;
  959.  
  960.     ## ################################################################# ##
  961.     ## Is the message a real cmail message or just a chess game? $PGN_GAME
  962.     ## will have been set by calling get_games_from_file if it's real
  963.     ## ################################################################# ##
  964.  
  965.     print "Processing game message..." ;
  966.  
  967.     if ($PGN_GAME) {
  968.     ## ############################################################# ##
  969.     ## Restore the results file from the archive if
  970.     ## necessary. This is helpful if the user pipes in an old
  971.     ## message for take-back purposes or whatever.
  972.     ## ############################################################# ##
  973.  
  974.     if (! -f "$PGN_GAME.res") {
  975.         ## Find what date would have been used to create the archive
  976.         foreach (@games) {
  977.         if (/\[Date\s"(.*)"\]/) {
  978.             $date = $1 ;
  979.             last ;    ## Assume all dates are the same
  980.         }
  981.         }
  982.         ## Restore results file from archive directory if it exists
  983.         if ($date && (-f "$ARCDIR/$PGN_GAME.$date.archive")) {
  984.         if (system ("cp",
  985.                 "$ARCDIR/$PGN_GAME.$date.archive",
  986.                 "$PGN_GAME.res")) {
  987.             print stderr (  "\nWarning: couldn't restore results file"
  988.                   . " from archive\n") ;
  989.         } else {
  990.             print "restored results file from archive..." ;
  991.         }
  992.         }
  993.     }
  994.  
  995.     ## ############################################################# ##
  996.     ## Find existing results, if any.
  997.     ## ############################################################# ##
  998.  
  999.     local (@results) = &get_games_from_file ("$PGN_GAME.res") ;
  1000.  
  1001.     ## ############################################################# ##
  1002.     ## Parse each game
  1003.     ## ############################################################# ##
  1004.  
  1005.     foreach $game (@games) {
  1006.         next unless ($game) ;
  1007.  
  1008.         ($game_name, $game_num) = &get_game_name_and_number ($game) ;
  1009.  
  1010.         $result = 0 ;
  1011.         @game = split("\n", $game) ;
  1012.         foreach (@game) {
  1013.         if (/^\[(Black|White)\s*"[?]"\]$/) {
  1014.             $colour = $1;
  1015.             $PW_GCOS = &get_pw_gcos () unless ($PW_GCOS) ;
  1016.             $PW_GCOS = "$MY_NNAME" unless ($PW_GCOS) ;
  1017.             s/".*"/"$PW_GCOS"/ ;
  1018.             &debug ("Changed $colour tag to be $_") ;
  1019.         } elsif (/^\[((Black|White)NA)\s*"(.*)"\]$/) {
  1020.             $NA = $3 ;
  1021.             if ($NA eq "?") {
  1022.             if ($RETURN_ADDRESS) {
  1023.                 $NA = $RETURN_ADDRESS ;
  1024.             } else {
  1025.                 $NA = "??" ;
  1026.             }
  1027.             $_ = "[$1 \"$NA\"]" ;
  1028.             &debug ("Changed $1 tag.\n") ;
  1029.             }
  1030.             if ($2 eq "White") {
  1031.             $PGN_WHITENA = $NA ;
  1032.             &debug ("WhiteNA tag is \"$PGN_WHITENA\"\n") ;
  1033.             } else {
  1034.             $PGN_BLACKNA = $NA ;
  1035.             &debug ("BlackNA tag is \"$PGN_BLACKNA\"\n") ;
  1036.             }
  1037.         } elsif (/\[Result\s*"(.*)"\]$/) {
  1038.             if ($1 ne "*") {
  1039.             $result = 1 ;
  1040.             $finished ++ ;
  1041.             } else {
  1042.             $unfinished ++ ;
  1043.             }
  1044.         } elsif (/^(.*[^\d]+)?\d+[.]\s*([^\s*]*\s+)?[^\s.*]+(\s*\d+[.]\s*)?[\s*]*$/) {
  1045.             if ($2) {
  1046.             $to_play = "white" ;
  1047.             } else {
  1048.             $to_play = "black" ;
  1049.             }
  1050.             &debug ("$to_play to play\n") ;
  1051.         }
  1052.         }
  1053.  
  1054.         ## ######################################################### ##
  1055.         ## Reconstruct possibly edited game
  1056.         ## ######################################################### ##
  1057.  
  1058.         $game = join ("\n", @game) . "\n\n" ;
  1059.  
  1060.         ## ######################################################### ##
  1061.         ## Build up results array
  1062.         ## ######################################################### ##
  1063.  
  1064.         if ($result) {
  1065.         $results[$game_num] = $games[$game_num] ;
  1066.         $new_result = 1 ;
  1067.         } elsif ($results[$game_num]) {
  1068.         ## Deleting a result does actually make sense if the user
  1069.         ## pipes in an old message for take-back purposes or whatever
  1070.         $results[$game_num] = "" ;
  1071.         $delete_result = 1 ;
  1072.         }
  1073.  
  1074.         ## ######################################################### ##
  1075.         ## Remove old .out files
  1076.         ## ######################################################### ##
  1077.  
  1078.         unlink <$PGN_GAME.game.out.*> ;
  1079.  
  1080.         ## ######################################################### ##
  1081.         ## Write ongoing games to game file and append new results
  1082.         ## ######################################################### ##
  1083.  
  1084.         die   "cmail: Can't open file for writing:"
  1085.         . " \"$CMAILDIR/$PGN_GAME.game.in\"\n"
  1086.         unless open (gamefile, ">$PGN_GAME.game.in") ;
  1087.         &debug (@games) ;
  1088.         print gamefile @games ;
  1089.         close (gamefile) ;
  1090.     }
  1091.  
  1092.     ## ############################################################# ##
  1093.     ## Print how many finished/unfinished games were found
  1094.     ## ############################################################# ##
  1095.  
  1096.     printf ("%d unfinished %s and %d finished %s...",
  1097.         $unfinished, ($unfinished == 1) ? "game" : "games",
  1098.         $finished, ($finished == 1) ? "game" : "games") ;
  1099.  
  1100.     ## ############################################################# ##
  1101.     ## Write results back to results file if there were any results
  1102.     ## in the input
  1103.     ## ############################################################# ##
  1104.     
  1105.     if ($new_result || $delete_result) {
  1106.         die (  "cmail: Can't open results file for writing:"
  1107.          . "\"$CMAILDIR/$PGN_GAME.res\"\n")
  1108.         unless open (resfile, ">$PGN_GAME.res") ;
  1109.         print resfile @results ;
  1110.         close (resfile) ;
  1111.     }
  1112.  
  1113.     ## ############################################################# ##
  1114.     ## Archive results if there are no unfinished games
  1115.     ## ############################################################# ##
  1116.     
  1117.     @ARCHIVE = @results unless ($unfinished) ;
  1118.  
  1119.     ## ############################################################# ##
  1120.     ## Figure out return address if not known
  1121.     ## ############################################################# ##
  1122.  
  1123.     if (! $RETURN_ADDRESS) {
  1124.         if ($to_play eq "black") {
  1125.         $RETURN_ADDRESS = $PGN_WHITENA unless ("$PGN_WHITENA" eq "?") ;
  1126.         } else {
  1127.         $RETURN_ADDRESS = $PGN_BLACKNA unless ("$PGN_BLACKNA" eq "?") ;
  1128.         }
  1129.     }
  1130.  
  1131.     ## ############################################################# ##
  1132.     ## Decide to include position diagrams in output if not already
  1133.     ## decided and a position diagram was found in the input
  1134.     ## ############################################################# ##
  1135.  
  1136.     if ("$OUTPUT_POS" eq "") {
  1137.         if (grep (/$posdiag/, @games)) {
  1138.         $OUTPUT_POS = "y" ; # Output position only if it was input
  1139.         } else {
  1140.         $OUTPUT_POS = "n" ;
  1141.         }
  1142.     }
  1143.     $ENV{'CMAIL_OUTPUT_POS'} = $OUTPUT_POS ;
  1144.  
  1145.     ## ############################################################# ##
  1146.     ## Check that we have enough info about the players to continue
  1147.     ## ############################################################# ##
  1148.  
  1149.     &find_game () ;
  1150.     } else {
  1151.     ## ############################################################# ##
  1152.     ## Set up xboard for viewing non-cmail PGN file
  1153.     ## ############################################################# ##
  1154.  
  1155.     local ($file) = "unknown.cmail" ;
  1156.     print "done.\nDumping non-cmail file into $CMAILDIR/$file..." ;
  1157.  
  1158.     die "cmail: Can't open file for writing: \"$CMAILDIR/$file\"\n"
  1159.         unless open (gamefile, ">$file") ;
  1160.     print gamefile @games ;
  1161.     close (gamefile) ;
  1162.     }
  1163.  
  1164.     print "done.\n" ;
  1165. }
  1166. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1167.  
  1168.  
  1169. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1170. ## Sort two filenames by the numeric suffix
  1171. sub sort_by_numeric_suffix {
  1172.     $a =~ /[.](\d+)$/ ; local ($na) = $1 ;
  1173.     $b =~ /[.](\d+)$/ ; local ($nb) = $1 ;
  1174.  
  1175.     return ($na <=> $nb) ;
  1176. }
  1177. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1178.  
  1179.  
  1180. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1181. sub get_game_name_and_number {
  1182.     local ($game) = join ("\n", @_) ;
  1183.  
  1184.     local ($game_name, $game_num) ;
  1185.  
  1186.     die "CMailGameName tag missing\n"
  1187.     unless ($game =~ /\[C[Mm]ailGameName\s+"(.*)"\]/) ;
  1188.  
  1189.     ## ################################################################# ##
  1190.     ## Set game name and number
  1191.     ## ################################################################# ##
  1192.  
  1193.     $game_name = $1 ;
  1194.     if ($game_name =~ s/^(.*)[.](\d+)$/$1/) {
  1195.     $game_num = $2 ;
  1196.     } else {
  1197.     $game_num = 1 ;
  1198.     }
  1199.  
  1200.     ## ################################################################# ##
  1201.     ## Set $PGN_GAME as a side-effect or check validity
  1202.     ## ################################################################# ##
  1203.  
  1204.     if ($PGN_GAME) {
  1205.     die (  "cmail: Mismatched game names in input message:\n"
  1206.          . "\"$PGN_GAME\", \"$game_name\"\n")
  1207.         if ("$PGN_GAME" ne "$game_name") ;
  1208.     } else {
  1209.     $PGN_GAME = $game_name ;
  1210.     &debug ("PGN_GAME set to \"$PGN_GAME\"\n") ;
  1211.     }
  1212.  
  1213.     return ($game_name, $game_num) ;
  1214. }
  1215. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1216.  
  1217.  
  1218. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1219. ## Read in a file of games and split into separate games
  1220. sub get_games_from_file {
  1221.     local ($file) = shift ;
  1222.  
  1223.     local (@file, $first_line) ; ## Slurp stdin
  1224.     if ($file eq "STDIN") {
  1225.     if ($first_line = <STDIN>) { ## Necessary to handle no input case
  1226.         @file = <STDIN> ; ## Slurp stdin
  1227.         @file = ($first_line, @file) ;
  1228.  
  1229.         foreach (@file) {
  1230.         ## Strip off leading quotation characters
  1231.         s/^[^\s]*>// ;
  1232.         s/^[ \t]+// ;
  1233.         
  1234.         ## Find return address and set it as a side-effect
  1235.         if (   /^From:?.*<([^>]+)>.*\n$/
  1236.             || /^From:? *([^ ]*).*\n$/) {
  1237.             $RETURN_ADDRESS = $1 ; ## Default for opp's email
  1238.             &debug ("Found opponent's email address",
  1239.                 " \"$RETURN_ADDRESS\"\n") ;
  1240.         } elsif (/\[C[Mm]ailGameName\s+"(.*)"\]/) {
  1241.             $PGN_GAME =  $1 ;
  1242.             $PGN_GAME =~ s/[.]\d+$// ;
  1243.         }
  1244.         }
  1245.  
  1246.         return (@file) unless ($PGN_GAME) ;
  1247.         if (grep (/\{--------------|\[Event/, @file)) {
  1248.         shift (@file) while ($file[0] !~ /\{--------------|\[Event/) ;
  1249.         }
  1250.     } else {
  1251.         return () ;
  1252.     }
  1253.     } else {
  1254.     return () unless (open (file, "<$file")) ;
  1255.     
  1256.     @file = <file> ; ## Slurp file
  1257.     close (file) ;
  1258.     }
  1259.     
  1260.     local (@games, $game_name, $game_num, $game, $tag) ;
  1261.  
  1262.     ## ################################################################# ##
  1263.     ## Remove headers and leading blanks
  1264.     ## ################################################################# ##
  1265.  
  1266.     local (@tgames) = split (/($posdiag\[Event|\[Event)/, join ('', @file)) ;
  1267.     shift (@tgames) while (!$tgames[0]) ;
  1268.  
  1269.     ## ################################################################# ##
  1270.     ## Set up @games array with proper game numbers
  1271.     ## ################################################################# ##
  1272.  
  1273.     while (@tgames) {
  1274.     $game = shift (@tgames) . shift (@tgames) ;
  1275.  
  1276.     ($game_name, $game_num) = &get_game_name_and_number ($game) ;
  1277.     $games[$game_num] = $game ;
  1278.     }
  1279.  
  1280.     return (@games) ;
  1281. }
  1282. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1283.  
  1284.  
  1285. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1286. ## Analyse output files and send the move
  1287. sub send_move {
  1288.     &debug ("Called <send_move>\n") ;
  1289.  
  1290.     ## ################################################################# ##
  1291.     ## Cat the pos (if it exists), game and result (if it exists) files
  1292.     ## into the .out file.
  1293.     ## ################################################################# ##
  1294.     
  1295.     local ($unfinished, $finished) = (0, 0) ;
  1296.     local (@results, $move, $move_msg, $to_play, $number) ;
  1297.    
  1298.     ## ################################################################# ##
  1299.     ## Find any .game.out.* files
  1300.     ## ################################################################# ##
  1301.  
  1302.     local (@outfiles) = (<$PGN_GAME.game.out.*>) ;
  1303.     @outfiles = grep (/[.]\d+$/, @outfiles) ; ## Ignore autosave files
  1304.     @outfiles = sort sort_by_numeric_suffix @outfiles ; ## Sort
  1305.  
  1306.     ## ################################################################# ##
  1307.     ## Find .res file if it exists
  1308.     ## ################################################################# ##
  1309.  
  1310.     local ($resfile) = "$PGN_GAME.res" ;
  1311.     if (-f $resfile) {
  1312.     @results = &get_games_from_file($resfile) ;
  1313.     &debug ("Read in results\n") ;
  1314.     } else {
  1315.     @results = () ;
  1316.     &debug ("No results to read\n") ;
  1317.     }
  1318.  
  1319.     ## ################################################################# ##
  1320.     ## Find .out file if it exists
  1321.     ## ################################################################# ##
  1322.  
  1323.     local ($outfile) = "$PGN_GAME.out" ;
  1324.     if (! ($REMAIL || $LOAD_XBOARD)) {
  1325.     @games = &play_on_tty (@results) ;
  1326.     } elsif (@outfiles) {
  1327.     foreach (@outfiles) {
  1328.         die "Can't open game file \"$_\" for reading"
  1329.         unless (open (game, "<$_")) ;
  1330.         die "Empty game file \"$_\""
  1331.         unless ($game = join ('', <game>)) ;
  1332.         close (game) ;
  1333.         &debug ("Read in game file \"$_\"\n") ;
  1334.         ## Remove position diagram if it wasn't in the input msg
  1335.         $game =~ s/($posdiag)// if ("$OUTPUT_POS" ne "y") ;
  1336.         ($game_name, $game_num) = &get_game_name_and_number ($game) ;
  1337.         $games[$game_num] = $game ;
  1338.     }
  1339.     &debug ("Read in games from output files\n") ;
  1340.     } else {
  1341.     &debug ("No games to read from STDIN\n") ;
  1342.     if (-f $outfile) {
  1343.         @games = &get_games_from_file($outfile) ;
  1344.     } else {
  1345.         die "Can't find any game files\n" unless (@results) ;
  1346.     }
  1347.     }
  1348.  
  1349.     ## ################################################################# ##
  1350.     ## Process games
  1351.     ## ################################################################# ##
  1352.  
  1353.     if (@games) {
  1354.     ## ############################################################# ##
  1355.     ## Find opponent's email address in games
  1356.     ## ############################################################# ##
  1357.     
  1358.     $OPP_ADDRESS = &get_opp_address_from_games (@games)
  1359.         unless ($OPP_ADDRESS) ;
  1360.  
  1361.     ## ############################################################# ##
  1362.     ## Collect the .game.out.* files into the .out file, remembering
  1363.     ## the move number of the last line and whether result or not
  1364.     ## ############################################################# ##
  1365.  
  1366.     $unfinished = 0 ;
  1367.     $move_num = 0 ;
  1368.     $move = blank ;
  1369.  
  1370.     ## Write games to output file
  1371.     die "Can't open output file \"$PGN_GAME.out\" for writing\n"
  1372.         unless open (outfile, ">$PGN_GAME.out") ;
  1373.     print outfile @games ;
  1374.     close (outfile) ;
  1375.         
  1376.     $game_num = -1 ;
  1377.     $num_games = 0 ;
  1378.     foreach $game (@games) {
  1379.         $game_num ++ ;
  1380.         next unless ($game) ;
  1381.  
  1382.         $num_games ++ ;
  1383.         ## Determine last move and whether result or not
  1384.         $result = 0 ;
  1385.         foreach (split(/\n/, $game)) {
  1386.         if (/^(.*[^\d]+|)(\d+)[.]+\s*[^\s.]*\s+(\S+)\s*$/) {
  1387.             $move_num = $2 ;
  1388.             $move    = $3 ;
  1389.         } elsif (/^\[Result\s*"(.*)"\]$/) {
  1390.             if ($1 ne "*") {
  1391.             $result = 1 ;
  1392.             $finished ++ ;
  1393.             } else {
  1394.             $unfinished ++ ;
  1395.             }
  1396.         }
  1397.         }
  1398.         
  1399.         $results[$game_num] = $game if ($result) ;
  1400.     }
  1401.  
  1402.     ## Write result files back to $PGN_GAME.res
  1403.     if (@results) {
  1404.         die "Can't open results file $PGN_GAME.res for writing\n"
  1405.         unless open (resfile, ">$PGN_GAME.res") ;
  1406.         print resfile @results ;
  1407.         close (results) ;
  1408.     }
  1409.  
  1410.     unlink <$PGN_GAME.game.out.*> ;
  1411.  
  1412.     ## ############################################################# ##
  1413.     ## Just say how many games are in the message
  1414.     ## ############################################################# ##
  1415.         
  1416.     if ($num_games > 1) {
  1417.         $move_msg = "$num_games games" ;
  1418.     } else {
  1419.         $move_msg = "1 game" ;
  1420.     }
  1421.  
  1422.     ## ############################################################# ##
  1423.     ## Print how many finished/unfinished games were found
  1424.     ## ############################################################# ##
  1425.  
  1426.     printf ("Sending %d unfinished %s and %d finished %s.\n",
  1427.         $unfinished, ($unfinished == 1) ? "game" : "games",
  1428.         $finished, ($finished == 1) ? "game" : "games") ;
  1429.  
  1430.     ## ############################################################# ##
  1431.     ## Send the mail message to opponent's address unless bypassed
  1432.     ## ############################################################# ##
  1433.  
  1434.     if ($SEND_MAIL) {
  1435.         local ($subject) = "cmail $move_msg <$PGN_GAME>" ;
  1436.           if (open (mail, "|$MAILPROG -s \"$subject\" $OPP_ADDRESS")) {
  1437.         print mail @games ;
  1438.         close (mail) ;
  1439.         print (  "Mailed cmail message to \"$OPP_ADDRESS\":\n"
  1440.                . "$move_msg <$PGN_GAME>\n") ;
  1441.         @ARCHIVE = @results unless ($unfinished) ;
  1442.         } else {
  1443.         die "Failed to mail cmail message.\n" ;
  1444.         }
  1445.     } else {
  1446.         print (  "Email not sent (as requested).\n"
  1447.            . "Would have mailed cmail message to \"$OPP_ADDRESS\":\n"
  1448.            . "$NUM_GAMES games <$PGN_GAME>\n") ;
  1449.     }
  1450.     } else {
  1451.     if (@results) {
  1452.         print "Email not sent (the game is over).\n" ;
  1453.     } else {
  1454.         die "No games found\n" ;
  1455.     }
  1456.     }
  1457. }
  1458. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1459.  
  1460.  
  1461. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1462. ## Get date from array of games
  1463. sub get_date_from_games {
  1464.     local (@games) = @_ ;
  1465.  
  1466.     local ($date) = "nodate" ;
  1467.  
  1468.     foreach (@games) {
  1469.     if (/\[Date\s"(.*)"\]/) {
  1470.         $date = $1 ;
  1471.         last ;        ## Assume all dates are the same
  1472.     }
  1473.     }
  1474.  
  1475.     return ($date) ;
  1476. }
  1477. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1478.  
  1479.  
  1480. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1481. ## Determine which colour is to play and the move number
  1482. sub get_to_play {
  1483.     &debug ("Called <get_to_play>\n") ;
  1484.     local ($game) = shift ;
  1485.  
  1486.     local ($number, $to_play) = (1, "White") ;
  1487.  
  1488.     $game =~ s/{[^}]*}//g ;
  1489.     $game =~ s/[\s\n]*[*][\s\n]*$// ;
  1490.  
  1491.     if ($game =~ /(\d+)[.][ ]*([.]*)[ ]*([^\n.]*)[\n\s]*[10-]*[\n\s]*$/) {
  1492.     $number = $1 ;
  1493.  
  1494.     if ($game =~ /\[Result "0-1"\]/) {
  1495.         $to_play = "Black" ;
  1496.     } elsif ($game =~ /\[Result "1-0"\]/) {
  1497.         $to_play = "White" ;
  1498.     } elsif (($2 ne "") || (($3 =~ / /) || ($3 eq ""))) {
  1499.         $to_play = "White" ;
  1500.         $number ++ ;
  1501.     } else {
  1502.         $to_play = "Black" ;
  1503.     }
  1504.     }
  1505.  
  1506.     &debug ("$to_play to play on move $number\n") ;
  1507.  
  1508.     return ($number, $to_play) ;
  1509. }
  1510. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1511.  
  1512.  
  1513. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1514. ## Get opp address from array of games
  1515. sub get_opp_address_from_games {
  1516.     &debug ("Called <get_opp_address_from_games>\n") ;
  1517.     local (@games) = @_ ;
  1518.  
  1519.     local ($opp_address) = "" ;
  1520.     local ($number, $to_play, $tag) ;
  1521.  
  1522.     foreach (@games) {
  1523.     next unless $_ ;
  1524.     
  1525.     ($number, $to_play) = &get_to_play ($_) ;
  1526.     $tag = (  ($to_play eq "White")
  1527.         ? "WhiteNA"
  1528.         : "BlackNA") ;
  1529.  
  1530.     if (/\[$tag\s"(.*)"\]/) {
  1531.         $opp_address = $1 ;
  1532.         die "cmail: Empty \"$tag\" tag\n" unless ($opp_address) ;
  1533.         &debug ("Found opponent's address \"$opp_address\" from games.\n") ;
  1534.         last ;        ## Assume all opp addresses are the same
  1535.     } else {
  1536.         die "cmail: Can't find \"$tag\" tag\n" ;
  1537.     }
  1538.     }
  1539.  
  1540.     die "cmail: Can't find opponent's email address\n" unless ($opp_address) ;
  1541.  
  1542.     return ($opp_address) ;
  1543. }
  1544. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1545.  
  1546.  
  1547. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1548. ## Archive @ARCHIVE in the $ARCDIR directory
  1549. sub archive {
  1550.     return () unless (@ARCHIVE) ;
  1551.  
  1552.     local ($date) = &get_date_from_games (@ARCHIVE) ;
  1553.  
  1554.     local ($file) = "$ARCDIR/$PGN_GAME.$date.archive" ;
  1555.     if (open (archive, ">$file")) {
  1556.     print archive @ARCHIVE ;
  1557.     close (archive) ;
  1558.     print "Archived game in $file\n" ;
  1559.     local (@remove) = <$PGN_GAME*> ;
  1560.     @remove = grep ($_ ne "$PGN_GAME.$date.archive",
  1561.             @remove) ; ## Don't delete archive
  1562.     unlink (@remove) ;
  1563.     } else {
  1564.     print "Couldn't open \"$file\" to archive game\n" ;
  1565.     }
  1566. }
  1567. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1568.  
  1569.  
  1570. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1571. ## Main body
  1572. sub main {
  1573.     local (@games) = () ;
  1574.  
  1575.     &initialise () ;        ## Initialise variables etc.
  1576.  
  1577.     if (-t || $REMAIL) { ## No input directed (invoked from a shell rather than a mailer)
  1578.     &debug ("Interactive!\n") ;
  1579.     &find_game () ;        ## Get the necessary info about the game
  1580.     } else {
  1581.     &debug ("Piping!\n") ;
  1582.     &analyse_email_message () ; ## Analyse the mail message
  1583.     }
  1584.     
  1585.     if (! &play_game ()) {    ## Load the game
  1586.     &send_move () ;        ## Analyse output and send moves
  1587.     }
  1588.  
  1589.     &archive () ;        ## Archive games if all finished
  1590.  
  1591.     close (tty) ;        ## Tidy up
  1592.     close (logfile) if ($DEBUG) ; ## Tidy up
  1593. }
  1594. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1595. &main () ;
  1596. #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=#
  1597. __END__
  1598. cmail $Revision: 3.8 $, Copyright (C) 1993 Free Software Foundation, Inc.
  1599. cmail comes with ABSOLUTELY NO WARRANTY; for details type `cmail -w'.
  1600. cmail is free software, and you are welcome to redistribute it
  1601. under certain conditions; type `cmail -c' for details.
  1602.  
  1603. {END OF GPL COPYRIGHT}
  1604.             GNU GENERAL PUBLIC LICENSE
  1605.    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  1606.  
  1607.   0. This License applies to any program or other work which contains
  1608. a notice placed by the copyright holder saying it may be distributed
  1609. under the terms of this General Public License.  The "Program", below,
  1610. refers to any such program or work, and a "work based on the Program"
  1611. means either the Program or any derivative work under copyright law:
  1612. that is to say, a work containing the Program or a portion of it,
  1613. either verbatim or with modifications and/or translated into another
  1614. language.  (Hereinafter, translation is included without limitation in
  1615. the term "modification".)  Each licensee is addressed as "you".
  1616.  
  1617. Activities other than copying, distribution and modification are not
  1618. covered by this License; they are outside its scope.  The act of
  1619. running the Program is not restricted, and the output from the Program
  1620. is covered only if its contents constitute a work based on the
  1621. Program (independent of having been made by running the Program).
  1622. Whether that is true depends on what the Program does.
  1623.  
  1624.   1. You may copy and distribute verbatim copies of the Program's
  1625. source code as you receive it, in any medium, provided that you
  1626. conspicuously and appropriately publish on each copy an appropriate
  1627. copyright notice and disclaimer of warranty; keep intact all the
  1628. notices that refer to this License and to the absence of any warranty;
  1629. and give any other recipients of the Program a copy of this License
  1630. along with the Program.
  1631.  
  1632. You may charge a fee for the physical act of transferring a copy, and
  1633. you may at your option offer warranty protection in exchange for a fee.
  1634.  
  1635.   2. You may modify your copy or copies of the Program or any portion
  1636. of it, thus forming a work based on the Program, and copy and
  1637. distribute such modifications or work under the terms of Section 1
  1638. above, provided that you also meet all of these conditions:
  1639.  
  1640.     a) You must cause the modified files to carry prominent notices
  1641.     stating that you changed the files and the date of any change.
  1642.  
  1643.     b) You must cause any work that you distribute or publish, that in
  1644.     whole or in part contains or is derived from the Program or any
  1645.     part thereof, to be licensed as a whole at no charge to all third
  1646.     parties under the terms of this License.
  1647.  
  1648.     c) If the modified program normally reads commands interactively
  1649.     when run, you must cause it, when started running for such
  1650.     interactive use in the most ordinary way, to print or display an
  1651.     announcement including an appropriate copyright notice and a
  1652.     notice that there is no warranty (or else, saying that you provide
  1653.     a warranty) and that users may redistribute the program under
  1654.     these conditions, and telling the user how to view a copy of this
  1655.     License.  (Exception: if the Program itself is interactive but
  1656.     does not normally print such an announcement, your work based on
  1657.     the Program is not required to print an announcement.)
  1658.  
  1659. These requirements apply to the modified work as a whole.  If
  1660. identifiable sections of that work are not derived from the Program,
  1661. and can be reasonably considered independent and separate works in
  1662. themselves, then this License, and its terms, do not apply to those
  1663. sections when you distribute them as separate works.  But when you
  1664. distribute the same sections as part of a whole which is a work based
  1665. on the Program, the distribution of the whole must be on the terms of
  1666. this License, whose permissions for other licensees extend to the
  1667. entire whole, and thus to each and every part regardless of who wrote it.
  1668.  
  1669. Thus, it is not the intent of this section to claim rights or contest
  1670. your rights to work written entirely by you; rather, the intent is to
  1671. exercise the right to control the distribution of derivative or
  1672. collective works based on the Program.
  1673.  
  1674. In addition, mere aggregation of another work not based on the Program
  1675. with the Program (or with a work based on the Program) on a volume of
  1676. a storage or distribution medium does not bring the other work under
  1677. the scope of this License.
  1678.  
  1679.   3. You may copy and distribute the Program (or a work based on it,
  1680. under Section 2) in object code or executable form under the terms of
  1681. Sections 1 and 2 above provided that you also do one of the following:
  1682.  
  1683.     a) Accompany it with the complete corresponding machine-readable
  1684.     source code, which must be distributed under the terms of Sections
  1685.     1 and 2 above on a medium customarily used for software interchange; or,
  1686.  
  1687.     b) Accompany it with a written offer, valid for at least three
  1688.     years, to give any third party, for a charge no more than your
  1689.     cost of physically performing source distribution, a complete
  1690.     machine-readable copy of the corresponding source code, to be
  1691.     distributed under the terms of Sections 1 and 2 above on a medium
  1692.     customarily used for software interchange; or,
  1693.  
  1694.     c) Accompany it with the information you received as to the offer
  1695.     to distribute corresponding source code.  (This alternative is
  1696.     allowed only for noncommercial distribution and only if you
  1697.     received the program in object code or executable form with such
  1698.     an offer, in accord with Subsection b above.)
  1699.  
  1700. The source code for a work means the preferred form of the work for
  1701. making modifications to it.  For an executable work, complete source
  1702. code means all the source code for all modules it contains, plus any
  1703. associated interface definition files, plus the scripts used to
  1704. control compilation and installation of the executable.  However, as a
  1705. special exception, the source code distributed need not include
  1706. anything that is normally distributed (in either source or binary
  1707. form) with the major components (compiler, kernel, and so on) of the
  1708. operating system on which the executable runs, unless that component
  1709. itself accompanies the executable.
  1710.  
  1711. If distribution of executable or object code is made by offering
  1712. access to copy from a designated place, then offering equivalent
  1713. access to copy the source code from the same place counts as
  1714. distribution of the source code, even though third parties are not
  1715. compelled to copy the source along with the object code.
  1716.  
  1717.   4. You may not copy, modify, sublicense, or distribute the Program
  1718. except as expressly provided under this License.  Any attempt
  1719. otherwise to copy, modify, sublicense or distribute the Program is
  1720. void, and will automatically terminate your rights under this License.
  1721. However, parties who have received copies, or rights, from you under
  1722. this License will not have their licenses terminated so long as such
  1723. parties remain in full compliance.
  1724.  
  1725.   5. You are not required to accept this License, since you have not
  1726. signed it.  However, nothing else grants you permission to modify or
  1727. distribute the Program or its derivative works.  These actions are
  1728. prohibited by law if you do not accept this License.  Therefore, by
  1729. modifying or distributing the Program (or any work based on the
  1730. Program), you indicate your acceptance of this License to do so, and
  1731. all its terms and conditions for copying, distributing or modifying
  1732. the Program or works based on it.
  1733.  
  1734.   6. Each time you redistribute the Program (or any work based on the
  1735. Program), the recipient automatically receives a license from the
  1736. original licensor to copy, distribute or modify the Program subject to
  1737. these terms and conditions.  You may not impose any further
  1738. restrictions on the recipients' exercise of the rights granted herein.
  1739. You are not responsible for enforcing compliance by third parties to
  1740. this License.
  1741.  
  1742.   7. If, as a consequence of a court judgment or allegation of patent
  1743. infringement or for any other reason (not limited to patent issues),
  1744. conditions are imposed on you (whether by court order, agreement or
  1745. otherwise) that contradict the conditions of this License, they do not
  1746. excuse you from the conditions of this License.  If you cannot
  1747. distribute so as to satisfy simultaneously your obligations under this
  1748. License and any other pertinent obligations, then as a consequence you
  1749. may not distribute the Program at all.  For example, if a patent
  1750. license would not permit royalty-free redistribution of the Program by
  1751. all those who receive copies directly or indirectly through you, then
  1752. the only way you could satisfy both it and this License would be to
  1753. refrain entirely from distribution of the Program.
  1754.  
  1755. If any portion of this section is held invalid or unenforceable under
  1756. any particular circumstance, the balance of the section is intended to
  1757. apply and the section as a whole is intended to apply in other
  1758. circumstances.
  1759.  
  1760. It is not the purpose of this section to induce you to infringe any
  1761. patents or other property right claims or to contest validity of any
  1762. such claims; this section has the sole purpose of protecting the
  1763. integrity of the free software distribution system, which is
  1764. implemented by public license practices.  Many people have made
  1765. generous contributions to the wide range of software distributed
  1766. through that system in reliance on consistent application of that
  1767. system; it is up to the author/donor to decide if he or she is willing
  1768. to distribute software through any other system and a licensee cannot
  1769. impose that choice.
  1770.  
  1771. This section is intended to make thoroughly clear what is believed to
  1772. be a consequence of the rest of this License.
  1773.  
  1774.   8. If the distribution and/or use of the Program is restricted in
  1775. certain countries either by patents or by copyrighted interfaces, the
  1776. original copyright holder who places the Program under this License
  1777. may add an explicit geographical distribution limitation excluding
  1778. those countries, so that distribution is permitted only in or among
  1779. countries not thus excluded.  In such case, this License incorporates
  1780. the limitation as if written in the body of this License.
  1781.  
  1782.   9. The Free Software Foundation may publish revised and/or new versions
  1783. of the General Public License from time to time.  Such new versions will
  1784. be similar in spirit to the present version, but may differ in detail to
  1785. address new problems or concerns.
  1786.  
  1787. Each version is given a distinguishing version number.  If the Program
  1788. specifies a version number of this License which applies to it and "any
  1789. later version", you have the option of following the terms and conditions
  1790. either of that version or of any later version published by the Free
  1791. Software Foundation.  If the Program does not specify a version number of
  1792. this License, you may choose any version ever published by the Free Software
  1793. Foundation.
  1794.  
  1795.   10. If you wish to incorporate parts of the Program into other free
  1796. programs whose distribution conditions are different, write to the author
  1797. to ask for permission.  For software which is copyrighted by the Free
  1798. Software Foundation, write to the Free Software Foundation; we sometimes
  1799. make exceptions for this.  Our decision will be guided by the two goals
  1800. of preserving the free status of all derivatives of our free software and
  1801. of promoting the sharing and reuse of software generally.
  1802. {END OF GPL CONDITIONS}
  1803.             GNU GENERAL PUBLIC LICENSE
  1804.                 NO WARRANTY
  1805.  
  1806.   11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  1807. FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
  1808. OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  1809. PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  1810. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  1811. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
  1812. TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
  1813. PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  1814. REPAIR OR CORRECTION.
  1815.  
  1816.   12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  1817. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  1818. REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  1819. INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  1820. OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  1821. TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  1822. YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  1823. PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  1824. POSSIBILITY OF SUCH DAMAGES.
  1825.  
  1826.